﻿@{
    ViewData["Title"] = await SystemConfig.GetByFieldNameAsync("SiteName");
}
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>@ViewData["Title"]</title>
    <link rel="stylesheet" href="~/lib/layui/css/layui.css">
    <link rel="stylesheet" href="~/admin/css/main.css">
    <style>
        html { zoom: 100%; width: 95%; margin: 5px auto; height: auto; }
    </style>
</head>
<body>

    <div class="layui-fluid" style="margin-top:20px;margin-bottom:50px;padding:10px;" id="noticeList">
    </div>

    <script src="~/lib/layui/layui.js"></script>
    <script>
        layui.define(function () {
            var element = layui.element, layer = layui.layer, $ = layui.jquery, util = layui.util;
            $.getJSON("/Admin/Notice/Index?json=1&readList=1", function (res) {
                if (res.data.length == 0) {
                    $('#noticeList').append('<div class="layui-card" ><div class="layui-card-header" style="overflow:hidden;"><h3>没有新信息！</h3></div></div>');
                } else {
                    $.each(res.data, function (index, value) {
                        $('#noticeList').append('<div class="layui-card" style="background-color:#eaf1fc"><div class="layui-card-header" style="overflow:hidden;"><h3>'
                            + '<a href="javascript:;" onclick="read(' + value.id + ',' + value.noticeId + ')">'
                            + Substr(value.title, 0, 20) + '</a></h3></div><div class="layui-card-body" style="background-color:#f7fcfe"><span style="font-size:12px;color:green;">'
                            + util.timeAgo(value.postTime) + '</span></div></div>');
                    });

                }
            });
        });

        function read(id, noticeId) {
            parent.layer.open({
                type: 2,
                title: "信息详情",
                area: ['800px', '500px'],
                closeBtn: 1, //不显示关闭按钮
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                content: "/Admin/Notice/Read?id=" + id + "&noticeid=" + noticeId
            });
        }

        function Substr(str, start, n) {
            if (str.replace(/[\u4e00-\u9fa5]/g, '**').length <= n) {
                return str;
            }
            let len = 0;
            let tmpStr = '';
            for (let i = start; i < str.length; i++) { // 遍历字符串
                if (/[\u4e00-\u9fa5]/.test(str[i])) { // 中文 长度为两字节
                    len += 2;
                } else {
                    len += 1;
                }
                if (len > n) {
                    break;
                } else {
                    tmpStr += str[i];
                }
            }
            return tmpStr + " ...";
        }

    </script>
</body>
</html>